package cn.cloudreal.cbms.common.interceptor;

import org.apache.log4j.Logger;

import cn.cloudreal.cbms.common.exception.ITCException;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.ParametersInterceptor;

/*******************************************************************************
 * Description：系统异常拦截器
 * 
 * @file: ITCInterceptor.java
 * @author: chendanggui
 * @version:1.0.0
 * @date: Jul 7, 2010 Copyright
 ******************************************************************************/
public class ITCInterceptor extends ParametersInterceptor
{

    private static final long serialVersionUID = 1L;

    /**
     * 日志类
     */
    private final Logger log = Logger.getLogger(ITCInterceptor.class);

    /**
     * 系统异常拦截器
     * 
     * @param actioninvocation
     * @return 返回值
     */
    @Override
    @SuppressWarnings("unchecked")
    public String intercept(ActionInvocation actioninvocation) throws Exception
    {

        log.info("Systems Exception interceptor start...");

        // 返回值
        String result = null;

        try
        {
            // Action 执行返回值
            result = actioninvocation.invoke();

        }
        catch (Exception e)
        {
            Object obj = actioninvocation.getAction();
            String msg = null;
            if (obj instanceof ActionSupport && e instanceof ITCException)
            {
                ActionSupport action = (ActionSupport) obj;
                ITCException je = (ITCException) e;
                msg = action.getText(je.getCode());
            }

            e.printStackTrace();
            throw new ITCException(msg);
        }
        log.info("Systems Exception interceptor end...");
        return result;

    }

}
